package com.ruoyi.order.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.constant.OrderConstants;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.order.api.domain.TOrder;
import com.ruoyi.order.service.ITOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 前台订单Controller
 *
 * @author isahino
 * @date 2023-09-23
 */
@RestController
@RequestMapping("/front_order")
public class FrontTOrderController {
    @Autowired
    private ITOrderService tOrderService;
    /**
     * 生成订单
     */
    @PostMapping("/{courseId}")
    public AjaxResult saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if ("".equals(memberId)) {
            return AjaxResult.error("当前未处于登录状态，请重新登录");
        }
        //创建订单,返回订单号
        String orderNo = tOrderService.createOrders(courseId, memberId);
        return StringUtils.isEmpty(orderNo) ? AjaxResult.error("创建订单失败，请稍后再试") : AjaxResult.success(orderNo);
    }

    /**
     * 根据订单号查询订单信息
     */
    @GetMapping("/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable String orderId) {
        LambdaQueryWrapper<TOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TOrder::getOrderNo, orderId);
        TOrder one = tOrderService.getOne(wrapper);
        return null == one? AjaxResult.error("订单号对应的订单不存在"): AjaxResult.success(one);
    }

    /**
     * 根据订单号更改订单支付状态
     * 因为没有真的开通微信支付功能，调用该方法视为支付
     */
    @GetMapping("/pay/{orderNo}")
    public AjaxResult queryPayStatus(@PathVariable String orderNo) {
        return tOrderService.paidOrder(orderNo, OrderConstants.PAID_TYPE_WX)? AjaxResult.success("支付成功"): AjaxResult.error("订单未支付或支付异常，请重新支付！");
    }

    /**
     * 会员获取自己的订单
     */
    @GetMapping("/member/{id}")
    public AjaxResult getAllOrderByMember(@PathVariable String id) {
        LambdaQueryWrapper<TOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TOrder::getMemberId, id);
        return AjaxResult.success(tOrderService.list(wrapper));
    }


    /**
     * 处理支付宝支付请求，响应支付页面
     * @param orderNo 订单号
     * @param httpResponse 响应
     */
    @GetMapping("/aliPay")
    public void aliPay(String orderNo, HttpServletResponse httpResponse) throws IOException {
        tOrderService.aliPay(orderNo, httpResponse);
    }

    /**
     * 处理支付宝回调请求
     * @param request 请求
     * @return 支付结果
     */
    @PostMapping("/notify")
    public AjaxResult payNotify(HttpServletRequest request) throws Exception {
        String orderNo = tOrderService.payNotify(request);
        if (null == orderNo) {
            return AjaxResult.error("订单未支付或支付异常，请重新支付！");
        }
        // 支付成功则更新订单状态并追加支付日志
        return tOrderService.paidOrder(orderNo, OrderConstants.PAID_TYPE_WX)? AjaxResult.success("支付成功"): AjaxResult.error("订单未支付或支付异常，请重新支付！");
    }
}
